<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>视频投屏服务器</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary: #4361ee;
            --primary-dark: #3a0ca3;
            --secondary: #4cc9f0;
            --accent: #f72585;
            --dark: #1e1e2e;
            --darker: #121220;
            --light: #f8f9fa;
            --gray: #6c757d;
            --success: #2ec4b6;
            --warning: #ff9e00;
            --danger: #e71d36;
            --border-radius: 12px;
            --shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
            --transition: all 0.3s ease;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background: linear-gradient(135deg, var(--darker), var(--dark));
            color: var(--light);
            min-height: 100vh;
            padding: 20px;
            line-height: 1.6;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        header {
            text-align: center;
            margin-bottom: 40px;
            padding: 20px 0;
            position: relative;
        }

        .logo {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 15px;
            margin-bottom: 15px;
        }

        .logo-icon {
            font-size: 2.8rem;
            color: var(--secondary);
            animation: pulse 2s infinite;
        }

        .logo h1 {
            font-size: 2.8rem;
            background: linear-gradient(to right, var(--secondary), var(--primary));
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
            font-weight: 800;
            letter-spacing: -0.5px;
        }

        .subtitle {
            font-size: 1.2rem;
            color: #a0d2eb;
            max-width: 600px;
            margin: 0 auto;
            opacity: 0.9;
        }

        .card {
            background: rgba(255, 255, 255, 0.05);
            backdrop-filter: blur(12px);
            border-radius: var(--border-radius);
            padding: 25px;
            margin-bottom: 30px;
            box-shadow: var(--shadow);
            border: 1px solid rgba(255, 255, 255, 0.08);
        }

        .card-title {
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 1.6rem;
            margin-bottom: 20px;
            color: var(--secondary);
        }

        .card-title i {
            font-size: 1.8rem;
        }

        .device-container {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            margin-bottom: 15px;
            align-items: center;
        }

        select {
            flex: 1;
            min-width: 200px;
            padding: 14px 18px;
            border-radius: var(--border-radius);
            border: none;
            background: rgba(0, 0, 0, 0.3);
            color: var(--light);
            font-size: 1rem;
            border: 1px solid rgba(255, 255, 255, 0.15);
            outline: none;
            transition: var(--transition);
            appearance: none;
            background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
            background-repeat: no-repeat;
            background-position: right 1rem center;
            background-size: 1em;
        }

        select:focus {
            border-color: var(--secondary);
            box-shadow: 0 0 0 3px rgba(76, 201, 240, 0.3);
        }

        .btn {
            padding: 14px 28px;
            border-radius: var(--border-radius);
            border: none;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: var(--transition);
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }

        .btn-primary {
            background: linear-gradient(135deg, var(--primary), var(--primary-dark));
            color: white;
        }

        .btn-primary:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 16px rgba(67, 97, 238, 0.4);
        }

        .btn-secondary {
            background: rgba(255, 255, 255, 0.1);
            color: var(--light);
            border: 1px solid rgba(255, 255, 255, 0.15);
        }

        .btn-secondary:hover {
            background: rgba(255, 255, 255, 0.2);
        }

        .btn-success {
            background: linear-gradient(135deg, var(--success), #25a99a);
            color: white;
        }

        .btn-success:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 16px rgba(46, 196, 182, 0.4);
        }

        .no-devices {
            text-align: center;
            padding: 30px 20px;
            background: rgba(231, 29, 54, 0.1);
            border-radius: var(--border-radius);
            border: 1px dashed rgba(231, 29, 54, 0.3);
            margin: 20px 0;
        }

        .no-devices p {
            margin-bottom: 20px;
            font-size: 1.1rem;
        }

        .video-list {
            list-style: none;
            margin-top: 15px;
        }

        .video-item {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 18px 20px;
            background: rgba(255, 255, 255, 0.03);
            border-radius: var(--border-radius);
            margin-bottom: 15px;
            transition: var(--transition);
            border-left: 4px solid transparent;
        }

        .video-item:hover {
            background: rgba(255, 255, 255, 0.08);
            border-left: 4px solid var(--secondary);
            transform: translateX(5px);
        }

        .video-info {
            display: flex;
            align-items: center;
            gap: 18px;
            flex: 1;
        }

        .video-icon {
            font-size: 1.8rem;
            color: var(--secondary);
            min-width: 40px;
            text-align: center;
        }

        .video-name {
            font-size: 1.1rem;
            word-break: break-word;
            flex: 1;
            line-height: 1.5;
        }

        .video-actions {
            display: flex;
            gap: 12px;
            flex-wrap: wrap;
        }

        .status-bar {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            background: rgba(0, 0, 0, 0.7);
            padding: 14px;
            text-align: center;
            font-size: 0.9rem;
            color: #a0d2eb;
            border-top: 1px solid rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(5px);
            z-index: 100;
        }

        .empty-state {
            text-align: center;
            padding: 50px 20px;
            color: var(--gray);
        }

        .empty-state i {
            font-size: 4rem;
            margin-bottom: 20px;
            color: rgba(255, 255, 255, 0.1);
        }

        .empty-state h3 {
            font-size: 1.8rem;
            margin-bottom: 10px;
            color: rgba(255, 255, 255, 0.4);
        }

        .empty-state p {
            font-size: 1.1rem;
            max-width: 500px;
            margin: 0 auto;
            color: rgba(255, 255, 255, 0.3);
        }

        .notification {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 18px 25px;
            border-radius: var(--border-radius);
            background: rgba(0, 0, 0, 0.8);
            border-left: 4px solid var(--success);
            color: white;
            transform: translateX(120%);
            transition: transform 0.4s ease;
            max-width: 350px;
            z-index: 1000;
            box-shadow: var(--shadow);
        }

        .notification.show {
            transform: translateX(0);
        }

        @media (max-width: 768px) {
            .container {
                padding: 10px;
            }
            
            .logo h1 {
                font-size: 2.2rem;
            }
            
            .subtitle {
                font-size: 1rem;
            }
            
            .card {
                padding: 20px 15px;
            }
            
            .device-container {
                flex-direction: column;
                align-items: stretch;
            }
            
            .video-item {
                flex-direction: column;
                align-items: flex-start;
                gap: 20px;
            }
            
            .video-actions {
                width: 100%;
                justify-content: flex-end;
            }
            
            .btn {
                width: 100%;
                padding: 16px;
            }
            
            .card-title {
                font-size: 1.4rem;
            }
        }

        @media (max-width: 480px) {
            .logo h1 {
                font-size: 1.9rem;
            }
            
            .logo-icon {
                font-size: 2.2rem;
            }
            
            .card-title {
                font-size: 1.3rem;
            }
            
            .video-name {
                font-size: 1rem;
            }
            
            .video-icon {
                font-size: 1.5rem;
            }
        }

        @keyframes pulse {
            0% { transform: scale(1); }
            50% { transform: scale(1.1); }
            100% { transform: scale(1); }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <div class="logo">
                <i class="fas fa-tv logo-icon"></i>
                <h1>视频投屏服务器</h1>
            </div>
            <p class="subtitle">浏览视频并将其投射到智能电视和流媒体设备</p>
        </header>

        <main>
            <div class="card">
                <h2 class="card-title">
                    <i class="fas fa-plug"></i>
                    可用设备
                </h2>
                
                {{if .Devices}}
                <div class="device-container">
                    <select id="deviceSelect">
                        {{range $key, $value := .Devices}}
                        <option value="{{$value}}">{{$key}}</option>
                        {{end}}
                    </select>
                    <button class="btn btn-primary" onclick="refreshDevices()">
                        <i class="fas fa-sync-alt"></i>
                        刷新设备
                    </button>
                </div>
                {{else}}
                <div class="no-devices">
                    <p>未检测到设备。确保您的设备位于同一网络上。</p>
                    <button class="btn btn-primary" onclick="refreshDevices()">
                        <i class="fas fa-sync-alt"></i>
                        刷新设备
                    </button>
                </div>
                {{end}}
            </div>

            <div class="card">
                <h2 class="card-title">
                    <i class="fas fa-film"></i>
                    视频库
                </h2>
                
                {{if .Videos}}
                <ul class="video-list">
                    {{range .Videos}}
                    <li class="video-item">
                        <div class="video-info">
                            <i class="fas fa-file-video video-icon"></i>
                            <span class="video-name">{{.}}</span>
                        </div>
                        <div class="video-actions">
                            <a href="/video?file={{.}}" class="btn btn-secondary">
                                <i class="fas fa-play"></i>
                                播放
                            </a>
                            <button class="btn btn-success" onclick="castVideo(this)">
                                <i class="fas fa-tv"></i>
                                投屏
                            </button>
                        </div>
                    </li>
                    {{end}}
                </ul>
                {{else}}
                <div class="empty-state">
                    <i class="fas fa-folder-open"></i>
                    <h3>没有视频文件</h3>
                    <p>将视频添加到您的库以后再开始</p>
                </div>
                {{end}}
            </div>
        </main>
        
        <div class="status-bar">
            <i class="fas fa-wifi"></i> 连接到本地网络 | <i class="fas fa-tv"></i> 已准备好投射到设备上
        </div>
        
        <div class="notification" id="notification">
            <i class="fas fa-check-circle"></i> <span id="notification-msg">投屏已成功开始！</span>
        </div>
    </div>

    <script>
        function castVideo(btn) {
            const file = btn.parentElement.parentElement.querySelector('.video-name').textContent;
            const deviceSelect = document.getElementById('deviceSelect');
            
            if (!deviceSelect || !deviceSelect.value) {
                showNotification("请先选择一个设备", "error");
                return;
            }
            
            const dmr = deviceSelect.value;
            const deviceName = deviceSelect.options[deviceSelect.selectedIndex].text;
            
            // Show casting feedback
            const originalText = btn.innerHTML;
            btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 正在投屏...';
            btn.disabled = true;
            
            fetch('/cast', {
                method: 'POST',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                body: `dmr=${encodeURIComponent(dmr)}&video=${encodeURIComponent(file)}`
            }).then(res => {
                if (res.ok) {
                    // Show success notification
                    showNotification(`正在投屏 "${truncateText(file, 20)}" to ${deviceName}`, "success");
                    
                    // Redirect to play page after a short delay
                    setTimeout(() => {
                        window.location.href = '/play';
                    }, 1500);
                } else {
                    res.text().then(msg => {
                        showNotification("投屏失败: " + msg, "error");
                    });
                }
            }).catch(err => {
                showNotification("网络错误: " + err.message, "error");
            }).finally(() => {
                btn.innerHTML = originalText;
                btn.disabled = false;
            });
        }

        function refreshDevices() {
            const btn = document.querySelector('.btn-primary');
            const originalText = btn.innerHTML;
            
            btn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 搜索中...';
            btn.disabled = true;
            
            fetch('/refresh-devices')
                .then(res => res.json())
                .then(devices => {
                    const select = document.getElementById('deviceSelect');
                    
                    if (select) {
                        select.innerHTML = '';
                        
                        if (Object.keys(devices).length === 0) {
                            // If there's a select element but no devices, remove it
                            select.parentElement.outerHTML = `
                                <div class="no-devices">
                                    <p>刷新后未检测到设备。确保您的设备位于同一网络上。</p>
                                    <button class="btn btn-primary" onclick="refreshDevices()">
                                        <i class="fas fa-sync-alt"></i>
                                        刷新设备
                                    </button>
                                </div>
                            `;
                        } else {
                            // Populate the select with new devices
                            for (const key in devices) {
                                const option = document.createElement('option');
                                option.value = devices[key];
                                option.textContent = key;
                                select.appendChild(option);
                            }
                            showNotification(`Found ${Object.keys(devices).length} devices`, "success");
                        }
                    } else {
                        // If no select exists (because no devices were present before)
                        const deviceContainer = document.querySelector('.device-container');
                        if (deviceContainer) {
                            if (Object.keys(devices).length === 0) {
                                showNotification("刷新后未找到设备", "warning");
                            } else {
                                // Create the select element
                                deviceContainer.innerHTML = `
                                    <select id="deviceSelect">
                                        ${Object.entries(devices).map(([key, value]) => 
                                            `<option value="${value}">${key}</option>`).join('')}
                                    </select>
                                    <button class="btn btn-primary" onclick="refreshDevices()">
                                        <i class="fas fa-sync-alt"></i>
                                        刷新设备
                                    </button>
                                `;
                                showNotification(`Found ${Object.keys(devices).length} devices`, "success");
                            }
                        }
                    }
                })
                .catch(err => {
                    showNotification("刷新设备时出错: " + err.message, "error");
                })
                .finally(() => {
                    btn.innerHTML = originalText;
                    btn.disabled = false;
                });
        }
        
        function showNotification(message, type) {
            const notification = document.getElementById('notification');
            const msgElement = document.getElementById('notification-msg');
            
            // Set message and style based on type
            msgElement.innerHTML = message;
            notification.style.borderLeftColor = type === "success" ? 
                "var(--success)" : type === "error" ? "var(--danger)" : "var(--warning)";
            
            // Show notification
            notification.classList.add('show');
            
            // Auto hide after 3 seconds
            setTimeout(() => {
                notification.classList.remove('show');
            }, 3000);
        }
        
        function truncateText(text, maxLength) {
            if (text.length <= maxLength) return text;
            return text.substring(0, maxLength) + '...';
        }
        
        // Initialize the page
        document.addEventListener('DOMContentLoaded', function() {
            // Add animation to the logo
            const logo = document.querySelector('.logo');
            logo.style.opacity = '0';
            logo.style.transform = 'translateY(-20px)';
            logo.style.transition = 'opacity 0.5s ease, transform 0.5s ease';
            
            setTimeout(() => {
                logo.style.opacity = '1';
                logo.style.transform = 'translateY(0)';
            }, 300);
        });
    </script>
</body>
</html>